package com.newlink.building.userinfo.activity

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.newlink.building.common_base.utils.NLog
import com.newlink.building.obfuscation.Module_Obfuscation_ObfuscationHelper
import com.newlink.building.obfuscation.Module_Obfuscation_ObfuscationInvoker
//import com.newlink.building.userinfo.BtOpenDoorService
//import com.newlink.building.userinfo.BtOpenDoorService1
import com.newlink.building.userinfo.Module_UserInfo_BtOpenDoorService2
import com.newlink.building.userinfo.Module_UserInfo_BtOpenDoorService2.Companion.SEND_OPEN_DOOR_ACTION

//import com.newlink.building.userinfo.SEND_OPEN_DOOR_ACTION


/**
 * @Author: Jake
 * @Date: 2024-04-24
 * @Description:
 */
class Module_UserInfo_PermissionRequestActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 权限请求页面初始化时的混淆
        Module_Obfuscation_ObfuscationHelper.onMethodEnter("PermissionRequestActivity", "onCreate")
        Module_Obfuscation_ObfuscationInvoker.invokeRandom("permission_request")

        // 请求蓝牙权限
        if (ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.BLUETOOTH_SCAN
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            NLog.e("[jake] :PermissionRequestActivity: -> request bt permission")
            ActivityCompat.requestPermissions(
                this,
                arrayOf<String>(Manifest.permission.BLUETOOTH_SCAN),
                REQUEST_CODE_BLUETOOTH
            )
        } else {
            // 权限已授予，可以执行相关操作
            finish()
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions!!, grantResults)
        // 权限结果处理时的混淆
        Module_Obfuscation_ObfuscationInvoker.invokeSpecific(13, requestCode)

        if (requestCode == REQUEST_CODE_BLUETOOTH) {
            if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                NLog.e("[jake] :PermissionRequestActivity: -> grant permission ")
//                val intent = Intent(this, BtOpenDoorService::class.java).apply {
                val intent = Intent(this, Module_UserInfo_BtOpenDoorService2::class.java).apply {
                    action = SEND_OPEN_DOOR_ACTION
                }
                startService(intent)
            } else {
                NLog.e("[jake] :PermissionRequestActivity: -> denied permission ")
                // 权限被拒绝，处理拒绝情况
            }
        }
        finish()
    }

    companion object {
        private const val REQUEST_CODE_BLUETOOTH = 1
    }
}